#include <iostream>
#include <string>
#include <sqlite3.h>

class SqliteHelper
{
private:
    typedef int(*SqliteCallback)(void*,int,char**,char**);

public:
    SqliteHelper(const std::string dbfile) :_dbfile(dbfile){}

    /*指定打开方式打开数据库文件*/
    int open(int flags=SQLITE_OPEN_FULLMUTEX)
    {
        int ret=sqlite3_open_v2(_dbfile.c_str(),&_pdb,SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|flags,nullptr);
        if(ret!=SQLITE_OK)
        {
            std::cout<<"创建/打开数据库失败: ";
            std::cout<<sqlite3_errmsg(_pdb)<<std::endl;
            return false;
        }
        return true;
    }

    bool exec(const std::string& query,SqliteCallback cb,void* arg)
    {
        int ret=sqlite3_exec(_pdb,query.c_str(),cb,arg,nullptr);
        if(ret!=SQLITE_OK)
        {
            std::cout<<"语句执行失败\n";
            return false;
        }
        return true;
    }

    void close()
    {
        if(_pdb!=nullptr) sqlite3_close_v2(_pdb);
    }

private:
    std::string _dbfile;
    sqlite3* _pdb;
};